export module FacilityLocation;

import DND;

import DefVar;
import Database;
import ResMgr;
import DefOrder;
import Facility;
import Common;

//定居点数据
export struct FacilityLocation
{
	DND_CLASS_TYPE("location", 0);

	enum Type
	{
		L_0CITY,//郡，区域行政中心
		L_1COUNTRY,//县，中级
		L_2VILLAGE,//乡，最小的地方
	};

	FacilityLocation(Facility* fac) :
		_fac{fac},
		_name{ "unknown" },
		_type{ 0 }
	{
		_ui._txtName = g_res->CreateTextMap();
	}

	FacilityLocation(Facility* fac, StreamInput& s) :
		_fac{fac}
	{
		s.Read(&_name);
		s.Read(&_type);

		_ui._txtName = g_res->CreateTextMap();
	}

	void Save(StreamOutput& s)
	{
		s.Write(&_name);
		s.Write(&_type);
	}

	struct UI
	{
		Text* _txtName;
		gcSprite _spr;
		UI() :_txtName{ nullptr } {}
		~UI() { delete _txtName; }
	};

	void RefreshUI()
	{
		_ui._spr = g_factory->CreateSprite(GetClassName(), _type);
		_ui._spr->SetOrder(DefOrder::FACILITY);//TODO： 根据y计算order
		
		_quadSpr = _ui._spr->GetQuad();
		/*_ui._spr->SetCoor(GetWorldCoor());
		Quad quad = _ui._spr->GetQuad();
		quad *= 0.5;

		_ui._spr->SetQuad(quad
			+ toVector2(_fac->GetSL()._xy) * DefVar::NODE_PIXEL
			+ Vector2{ DefVar::NODE_PIXEL * 0.5, DefVar::NODE_PIXEL * 0.5 });*/

		//
		_ui._txtName->SetString(lang(GetClassName(), _name));
	}

	void Render(const Vector2& pos, real scale)
	{
		if (scale < 0.2)
		{
			return;
		}

		if (scale <= 0.25)
		{
			if (_type > Type::L_0CITY)
			{
				return;
			}
			else
			{
				_ui._spr->SetQuad(_quadSpr * Vector2{ 0.5, 0.5 } + pos);
				_ui._txtName->SetFontSize(16);
			}
			
		}
		else if (scale <= 0.5)
		{
			_ui._spr->SetQuad(_quadSpr * Vector2{ 0.5, 0.5 } + pos);
			_ui._txtName->SetFontSize(18);
		}
		else if (scale <= 1.0)
		{
			_ui._spr->SetQuad(_quadSpr * Vector2{ 0.75, 0.75 } + pos);
			_ui._txtName->SetFontSize(20);
		}
		else
		{
			_ui._spr->SetQuad(_quadSpr + pos);
			_ui._txtName->SetFontSize(24);
		}
			

		

		_ui._txtName->SetCenter(pos);

		_ui._spr->Render();
		_ui._txtName->Render();
	}

	bool IsPickup()
	{
		return _ui._spr->IsPickup() || _ui._txtName->IsPickup();
	}

public:
	Facility* _fac;
	Quad _quadSpr;
	UI _ui;

	//data
	std::string _name;
	natural _type;
};
